package com.sdy.common.model;

import java.util.Collections;
import java.util.List;

/**
 * api中使用的分页工具
 * @author zhouziqiang 
 */
public class PageResult<T> extends PageQuery {
    /**
     * 查询数据列表
     */
    private List<T> records = Collections.emptyList();
    /**
     * 总数
     */
    private long total = 0;
    private long pages = 0;
    
    public PageResult() {
        
    }
    
    public PageResult(long current, long size, long total, List<T> records) {
        this(new PageQuery(current, size), total, records);
    }

    public PageResult(PageQuery pageQuery, long total, List<T> records) {
        super(pageQuery.getCurrent(), pageQuery.getSize());
        this.total = total < 0 ? 0 : total;
        this.records = records == null ? Collections.emptyList() : records;

        if (getSize() == 0) {
            this.pages = 0;
        }
        long pages = getTotal() / getSize();
        if (getTotal() % getSize() != 0) {
            pages++;
        }
        this.pages = pages;
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public long getPages() {
        return pages;
    }

    public void setPages(long pages) {
        this.pages = pages;
    }
}
